﻿
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using GruntXProductions;
using Viper;
using Viper.Opcodes;

namespace Diamond
{
    public partial class Parser
    {
        public IfStatement ParseIf()
        {
            readToken(); readToken();
            IfStatement ifstat = new IfStatement();
            ifstat.Comparison = ParseExpression();
            if (!(readToken() is Tokens.closeParenthesis))
                throw new ParserException("Expected )" + peekToken(-1).ToString());
            ifstat.Body = new CodeBlock();
            while (peekToken().ToString() != "end" && peekToken().ToString() != "else")
            {
                while (peekToken() is Tokens.EOL) readToken();

                ifstat.Body.Body.Add(ParseLine());
            }
            if (peekToken().ToString() == "else")
            {
                readToken();
                ifstat.Else = new CodeBlock();
                if (peekToken().ToString() == "if")
                    ifstat.Else.Body.Add(ParseIf());
                else
                {
                    while (peekToken().ToString() != "end")
                    {
                        ifstat.Else.Body.Add(ParseLine());
                    }
                    readToken();
                }
            }
            else
            {
                readToken();
            }

            return ifstat;
        }
    }
}